package com.ruoyi.ucar.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.ContractToolDetailQueryBoReq;
import com.ruoyi.ucar.boreq.ContractToolListQueryBoReq;
import com.ruoyi.ucar.boresp.ContractToolDetailQueryBoResp;
import com.ruoyi.ucar.boresp.ContractToolListQueryBoResp;
import com.ruoyi.ucar.common.CommonVoResp;
import com.ruoyi.ucar.domain.TContractTool;
import com.ruoyi.ucar.service.ITContractToolService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.util.ResponseUtil;
import com.ruoyi.ucar.voreq.*;
import com.ruoyi.ucar.voresp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 合同工具Controller
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Api(value = "车商代理合同管理",tags = "车商代理合同管理模块接口")
@RestController
@RequestMapping("/ucar/tool")
public class TContractToolController extends BaseController
{
    @Autowired
    private ITContractToolService tContractToolService;

    /**
     * 查询合同工具列表
     */
    @ApiOperation("车商合同工具列表")
    @PreAuthorize("@ss.hasPermi('ucar:tool:list')")
    @PostMapping("/list")
    public CommonVoResp<ContractToolListQueryVoResp> list(@RequestBody ContractToolListQueryVoReq contractToolListQueryVoReq)
    {
        logger.info("车商合同工具列表VO端入口参数:{}",contractToolListQueryVoReq);
        CommonVoResp<ContractToolListQueryVoResp> commonVoResp=new CommonVoResp<>();
        ContractToolListQueryVoResp contractToolListQueryVoResp=new ContractToolListQueryVoResp();
        List<ContractToolListQueryVo> contractToolListQueryVos=new ArrayList<>();
        if(
                contractToolListQueryVoReq.getPageIndex()==null||
                        contractToolListQueryVoReq.getPageSize()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        ContractToolListQueryBoReq contractToolListQueryBoReq=CommonUtil.objToObj(contractToolListQueryVoReq, ContractToolListQueryBoReq.class);
        contractToolListQueryBoReq.setPageIndex(contractToolListQueryVoReq.getPageIndex());
        contractToolListQueryBoReq.setPageSize(contractToolListQueryVoReq.getPageSize());
        logger.info("车商合同工具列表查询VO端VO===>BO结果：{}",contractToolListQueryVoReq);
        ContractToolListQueryBoResp contractToolListQueryBoResp=tContractToolService.queryContractToolByCond(contractToolListQueryBoReq);
        logger.info("车商合同工具列表查询VO端BO响应结果：{}",contractToolListQueryBoResp);
        contractToolListQueryVoResp.setTotalCount(contractToolListQueryBoResp.getTotalCount());
        contractToolListQueryVoResp.setTotalPage(contractToolListQueryBoResp.getTotalPage());
        contractToolListQueryBoResp.getContractToolListQueryBos().forEach(contractToolListQueryBo-> {
            ContractToolListQueryVo contractToolListQueryVo=  CommonUtil.objToObj(contractToolListQueryBo, ContractToolListQueryVo.class);
            contractToolListQueryVos.add(contractToolListQueryVo);
        });
        contractToolListQueryVoResp.setContractToolListQueryVos(contractToolListQueryVos);
        commonVoResp.setData(contractToolListQueryVoResp);
        logger.info("车商合同工具列表查询VO端VO返回结果：{}",commonVoResp);
        return commonVoResp;
    }


    /**
     * 查询合同工具列表
     */
    @ApiOperation("车商合同工具列表--Mobile使用")
    @Anonymous
    @PostMapping("/mobileList")
    public CommonVoResp<List<ContractToolListQueryVo>> mobileList( )
    {
        CommonVoResp<List<ContractToolListQueryVo>> commonVoResp=new CommonVoResp<>();
        List<ContractToolListQueryVo> contractToolListQueryVos=new ArrayList<>();
        ContractToolListQueryBoResp contractToolListQueryBoResp=tContractToolService.contractToolListMobileQuery();
        contractToolListQueryBoResp.getContractToolListQueryBos().forEach(contractToolListQueryBo-> {
            ContractToolListQueryVo contractToolListQueryVo=  CommonUtil.objToObj(contractToolListQueryBo, ContractToolListQueryVo.class);
            contractToolListQueryVos.add(contractToolListQueryVo);
        });
        logger.info("车商合同工具列表查询VO端BO响应结果：{}",contractToolListQueryBoResp);
        commonVoResp.setData(contractToolListQueryVos);
        logger.info("车商合同工具列表查询VO端VO返回结果：{}",commonVoResp);
        return commonVoResp;
    }


    /**
     * 导出合同工具列表
     */
    @PreAuthorize("@ss.hasPermi('ucar:tool:export')")
    @Log(title = "合同工具", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TContractTool tContractTool)
    {
        List<TContractTool> list = tContractToolService.selectTContractToolList(tContractTool);
        ExcelUtil<TContractTool> util = new ExcelUtil<TContractTool>(TContractTool.class);
        util.exportExcel(response, list, "合同工具数据");
    }

    /**
     * 获取合同工具详细信息
     */
    @ApiOperation("合同工具详细信息-id和contractToolUuid，两者传一即可")
    @PostMapping(value = "/detail")
    public CommonVoResp<ContractToolDetailQueryVoResp> detail(@RequestBody ContractToolDetailQueryVoReq contractToolDetailQueryVoReq)
    {
        CommonVoResp<ContractToolDetailQueryVoResp> commonVoResp=new CommonVoResp<>();
        logger.info("合同工具详细信息入参：{}",contractToolDetailQueryVoReq);
        if(
                contractToolDetailQueryVoReq.getContractToolUuid()==null&&
                        contractToolDetailQueryVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        ContractToolDetailQueryBoResp contractToolDetailQueryBoResp=tContractToolService.contractToolDetailQuery(CommonUtil.objToObj(contractToolDetailQueryVoReq, ContractToolDetailQueryBoReq.class));

        if(StringUtils.isNull(contractToolDetailQueryBoResp)){
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }else{
            ContractToolDetailQueryVoResp contractToolDetailQueryVoResp=CommonUtil.objToObj(contractToolDetailQueryBoResp,ContractToolDetailQueryVoResp.class);
            commonVoResp.setData(contractToolDetailQueryVoResp);
        }
        logger.info("获取合同工具详细信息出参：{}",commonVoResp);
        return commonVoResp;

    }

    /**
     * 新增合同工具
     */
    @ApiOperation("新增合同工具")
    @PostMapping("/add")
    public CommonVoResp<ContracrToolAddVoResp> add(ContractToolAddVoReq contractToolAddVoReq)
    {         CommonVoResp<ContracrToolAddVoResp> commonVoResp=new CommonVoResp<>();
              logger.info("新增合同工具入参：{}",contractToolAddVoReq);
        try {
                  ContracrToolAddVoResp contracrToolAddVoResp=new ContracrToolAddVoResp();
                  String contractToolUuid = tContractToolService.saveContractTool(contractToolAddVoReq);
                  contracrToolAddVoResp.setContractToolUuid(contractToolUuid);
                  commonVoResp.setData(contracrToolAddVoResp);
            } catch (IOException e) {
                  commonVoResp = ResponseUtil.setCommonVoRespErrorFile(commonVoResp);
                  return commonVoResp;
              }
              logger.info("新增合同工具出参：{}",commonVoResp);
              return commonVoResp;
    }

    /**
     * 修改合同工具
     */
    @ApiOperation("修改合同工具")
    @PreAuthorize("@ss.hasPermi('ucar:tool:edit')")
    @PostMapping("/update")
    public CommonVoResp<String> edit(ContractToolUpdateVoReq contractToolUpdateVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        logger.info("修改合同工具入参：{}",contractToolUpdateVoReq);
        try {
            return ResponseUtil.commonUpdateResult(tContractToolService.updateContractTool(contractToolUpdateVoReq),commonVoResp);
        } catch (IOException e) {
            return ResponseUtil.commonUpdateResult(0,commonVoResp);
        }
    }

    /**
     * 删除合同工具
     */
    @ApiOperation("删除合同工具-id和carouselImageUuid，两者传一即可")
    @PostMapping("/delete")
    public CommonVoResp<String> remove(@RequestBody DeleteContractToolVoReq deleteContractToolVoReq)
    {
        CommonVoResp<String> commonVoResp=new CommonVoResp<>();
        if(
                deleteContractToolVoReq.getContractToolUuid()==null&&
                        deleteContractToolVoReq.getId()==null){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        logger.info("删除合同工具入参：{}",deleteContractToolVoReq);
        return ResponseUtil.commonDeleteResult(tContractToolService.deletetTContractToolById(deleteContractToolVoReq.getContractToolUuid(),deleteContractToolVoReq.getId()),commonVoResp);
    }
}
